package com.ecarx.vhaltests.hy11.testcases.system.seatadjustment;

import static android.car.VehiclePropertyIds.SEAT_TILT_POS;

import android.car.VehicleAreaSeat;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;
import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

// [SW_FC_004_045]第一排座垫折叠调节1stRowSeatCushionTiltAdjustment - 实时位置
public class SeatTIltPosTest extends VhalUnitTestBase {
    public static final String TAG = "SeatTIltPosTest";
    private static int halValueRow1Left;
    private static int halStatusRow1Left;
    private static int halValueRow1Right;
    private static int halStatusRow1Right;
    final static Object lockRow1Left = new Object();
    final static Object lockRow1Right = new Object();
    private static final IpcpClient ipcpClient = new IpcpClient();
    @BeforeClass
    public static void testInit() {
        mCarPropertyMgr.registerCallback(carPropertyEventCallback, SEAT_TILT_POS, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        setConfig();
    }
    @Test
    public void testSeatTIltPosNotAvail() {
        setSeatTIltPosNotAvail();
        lockRow1Left();
        lockRow1Right();
        Assert.assertEquals(halStatusRow1Left, 1);
        Assert.assertEquals(halStatusRow1Right, 1);
        testSeatTIltPosNotAvailLeft();
        testSeatTIltPosNotAvailRight();
    }
    @Test
    public void testSeatTIltPosAvail() {
        setSeatTIltPosAvail();
        lockRow1Left();
        lockRow1Right();
        Assert.assertEquals(halStatusRow1Left, 0);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTIltPosIsAvailLeft();
        testSeatTIltPosIsAvailRight();
    }
    @Test
    public void testSeatTIltPosRow1Left50() {
        setSeatTIltPosAvail();
        setCDMDriver(50, 3);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, 50);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatTIltPosPosValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, 50);
    }
    @Test
    public void testSeatTIltPosRow1Left50QFNotGood() {//信号质量差的时候收到的百分比忽略
        setSeatTIltPosAvail();
        setCDMDriver(50, 3);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, 50);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatTIltPosPosValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, 50);
        setCDMDriver(60, 2);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, 50);
        testSeatTIltPosPosValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, 50);
    }
    @Test
    public void testSeatTIltPosRow1Right50() {
        setSeatTIltPosAvail();
        setCDMPass(50, 3);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, 50);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTIltPosPosValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, 50);
    }
    @Test
    public void testSeatTIltPosRow1Right50QFNotGood() {//信号质量差的时候收到的百分比忽略
        setSeatTIltPosAvail();
        setCDMPass(50, 3);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, 50);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTIltPosPosValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, 50);
        setCDMPass(50, 2);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, 50);
        testSeatTIltPosPosValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, 50);
    }

    public void testSeatTIltPosPosValue(int areaId, int value) {
        int halValue = mCarPropertyMgr.getIntProperty(SEAT_TILT_POS, areaId);
        Assert.assertEquals(halValue, value);
    }
    public void testSeatTIltPosIsAvailLeft() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_TILT_POS, VehicleAreaSeat.SEAT_ROW_1_LEFT);
        Assert.assertTrue(isAvail);
    }
    public void testSeatTIltPosIsAvailRight() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_TILT_POS, VehicleAreaSeat.SEAT_ROW_1_RIGHT);
        Assert.assertTrue(isAvail);
    }
    public void testSeatTIltPosNotAvailLeft() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_TILT_POS, VehicleAreaSeat.SEAT_ROW_1_LEFT);
        Assert.assertFalse(isAvail);
    }
    public void testSeatTIltPosNotAvailRight() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_TILT_POS, VehicleAreaSeat.SEAT_ROW_1_RIGHT);
        Assert.assertFalse(isAvail);
    }
    public void lockRow1Left() {
        try {
            synchronized (lockRow1Left) {
                lockRow1Left.wait(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void lockRow1Right() {
        try {
            synchronized (lockRow1Right) {
                lockRow1Right.wait(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    private static CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback = new CarPropertyManager.CarPropertyEventCallback() {
        @Override
        public void onChangeEvent(CarPropertyValue carPropertyValue) {
            try {
                Log.d(TAG, carPropertyValue.toString());
                int areaId = carPropertyValue.getAreaId();
                if (areaId == VehicleAreaSeat.SEAT_ROW_1_LEFT) {
                    halValueRow1Left = (int) carPropertyValue.getValue();
                    halStatusRow1Left = carPropertyValue.getStatus();
                    synchronized (lockRow1Left) {
                        lockRow1Left.notify();
                    }
                } else if (areaId == VehicleAreaSeat.SEAT_ROW_1_RIGHT) {
                    halValueRow1Right = (int) carPropertyValue.getValue();
                    halStatusRow1Right = carPropertyValue.getStatus();
                    synchronized (lockRow1Right) {
                        lockRow1Right.notify();
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
        @Override
        public void onErrorEvent(int i, int i1) {
        }
    };

    private static void setConfig() {
        int cemSvcId = 37;
        int[] cemOperIds = {999};
        int ccSvcId = 135;
        int[] ccOperIds = {200};
        int cc1000SvcId = 136;
        int[] cc1000OperIds = {200};
        int cdmSvcId = 36;
        int[] cdmOperIds = {999};
        boolean retCem = ipcpClient.setConfig(cemSvcId, cemOperIds);
        Assert.assertTrue(retCem);
        boolean retCc = ipcpClient.setConfig(ccSvcId, ccOperIds);
        Assert.assertTrue(retCc);
        boolean retCdm = ipcpClient.setConfig(cdmSvcId, cdmOperIds);
        Assert.assertTrue(retCdm);
        boolean retCc1000 = ipcpClient.setConfig(cc1000SvcId, cc1000OperIds);
        Assert.assertTrue(retCc1000);
    }

    public void setSeatTIltPosNotAvail() {
        setCarConfig(6, 5);
        setCarConfig1000(3, 6);
        setCEM(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_ABANDON);
    }

    public void setSeatTIltPosAvail() {
        setCarConfig(4, 2);
        setCarConfig1000(2, 2);
        setCDM(0, 2, 0, 2);
        setCEM(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_DRIVING);
    }

    public void setCEM(int carMode, int usgMode) {
        int cemSvcId = 37;
        int cemOperId = 999;
        String[] cems = {"vehModMngtGlbSafe1CarModSts1", "vehModMngtGlbSafe1UsgModSts"};
        int[] cemValues = {carMode, usgMode};
        boolean cemResult = ipcpClient.setCEMSignal(cemSvcId, cemOperId, cems, cemValues);
        Assert.assertTrue(cemResult);
        delay();
    }

    public void setCarConfig(int cc184, int cc468) {
        int ccSvcId = 135;
        int ccOperId = 200;
        int[] configs = {184, 468};
        int[] values = {cc184, cc468};
        boolean ccResult = ipcpClient.setCarconfigSignal(ccSvcId, ccOperId, configs, values);
        Assert.assertTrue(ccResult);
        delay();
    }

    private void setCarConfig1000(int cc1173, int cc1174) {
        int ccSvcId = 136;
        int ccOperId = 200;
        int[] configs = {1173, 1174};
        int[] values = {cc1173, cc1174};
        boolean ccResult = ipcpClient.setCarconfig1000Signal(ccSvcId, ccOperId, configs, values);
        Assert.assertTrue(ccResult);
        delay();
    }

    public void setCDMDriver(int drvrSeatPosPercSeatPosFrntHeiPerc, int drvrSeatPosPercSeatPosFrntHeiQF) {
        setCDM(drvrSeatPosPercSeatPosFrntHeiPerc, drvrSeatPosPercSeatPosFrntHeiQF, 0, 0);
    }

    public void setCDMPass(int passSeatPosPercSeatPosFrntHeiPerc, int passSeatPosPercSeatPosFrntHeiQF) {
        setCDM(0, 0, passSeatPosPercSeatPosFrntHeiPerc, passSeatPosPercSeatPosFrntHeiQF);
    }

    public void setCDM(int drvrSeatPosPercSeatPosFrntHeiPerc, int drvrSeatPosPercSeatPosFrntHeiQF, int passSeatPosPercSeatPosFrntHeiPerc, int passSeatPosPercSeatPosFrntHeiQF) {
        int cdmSvcId = 36;
        int cdmOperId = 999;
        String[] cdms = {"drvrSeatPosPercSeatPosFrntHeiPerc", "drvrSeatPosPercSeatPosFrntHeiQF", "passSeatPosPercSeatPosFrntHeiPerc", "passSeatPosPercSeatPosFrntHeiQF"};
        int[] cdmValues = {drvrSeatPosPercSeatPosFrntHeiPerc * 10, drvrSeatPosPercSeatPosFrntHeiQF, passSeatPosPercSeatPosFrntHeiPerc * 10, passSeatPosPercSeatPosFrntHeiQF};
        boolean cdmResult = ipcpClient.setCDMSignal(cdmSvcId, cdmOperId, cdms, cdmValues);
        Assert.assertTrue(cdmResult);
        delay();
    }
}



